package com.intel.two.service.smarthome.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.bo.smarthome.HouseholdBo;
import com.intel.two.common.R;
import com.intel.two.mapper.smarthome.HouseholdMapper;
import com.intel.two.pojo.smarthome.Guarantee;
import com.intel.two.pojo.smarthome.Household;
import com.intel.two.service.smarthome.HouseholdService;
import com.intel.two.vo.smarthome.HouseholdVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @BelongsProject: intelligentcommunity
 * @BelongsPackage: com.intel.two.service.smarthome.impl
 * @Author: yanhongwei
 * @CreateTime: 2022-10-14  14:45
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class HouseholdServiceImpl implements HouseholdService {

    @Autowired
    private HouseholdMapper householdMapper;
    @Override
    public R householdList(Map<String, Object> parameters) {
        Integer limit = Integer.parseInt((String) parameters.get("limit"));
        Integer page = Integer.parseInt((String) parameters.get("page"));
//        开启分页
        PageHelper.startPage(page, limit);
        List<Household> households = householdMapper.householdList(parameters);
        Map<String, Object> map = new HashMap<>();
//        获取分页的相关信息
        PageInfo<Household> pageInfo = new PageInfo<>(households);
        map.put("households", households);//学生信息
        map.put("count", pageInfo.getTotal());//总记录数
        return R.ok().data(map);
    }

    @Override
    public R householdList1(Map<String, Object> parameters) {
        Integer limit = Integer.parseInt((String) parameters.get("limit"));
        Integer page = Integer.parseInt((String) parameters.get("page"));
//        开启分页
        PageHelper.startPage(page, limit);
        List<HouseholdVo> households = householdMapper.householdList1(parameters);
        Map<String, Object> map = new HashMap<>();
//        获取分页的相关信息
        PageInfo<HouseholdVo> pageInfo = new PageInfo<>(households);
        map.put("households", households);//家居信息
        map.put("count", pageInfo.getTotal());//总记录数
        return R.ok().data(map);
    }

    @Override
    public R householdAdd(Household household) {

        int i = householdMapper.householdAdd(household);




        return i>0?R.ok():R.error();


    }

    @Override
    public R householdEdit(Household household) {
        int i = householdMapper.householdEdit(household);

        return i>0?R.ok():R.error();
    }

    @Override
    public R househomesDel(List<Household> householdList) {
        List<Integer> collect = householdList.stream().map(household -> household.getHousehold_id()).collect(Collectors.toList());
        int i = householdMapper.househomesDel(collect);
        return i>0?R.ok():R.error();
    }

    @Override
    public R househomeDel(Household household) {

        int i = householdMapper.househomeDel(household);
        return i>0?R.ok():R.error();
    }

    @Override
    public R echarts1() {

       List<HouseholdVo> householdVoList =  householdMapper.echarts1();

        return  R.ok().data("householdVoList", householdVoList);
    }

    @Override
    public List<HouseholdBo> householdExcelData(Household household) {


//        HouseholdBo householdBo = new HouseholdBo();
//        BeanUtils.copyProperties(stu,householdBo);
        List<Household> households = householdMapper.householdExcelData(household);
        //把po 转为 bo
        List<HouseholdBo> collect = households.stream().map(new Function<Household, HouseholdBo>() {
            @Override
            public HouseholdBo apply(Household household) {
                HouseholdBo householdBo = new HouseholdBo();
                BeanUtils.copyProperties(household, householdBo);
                return householdBo;
            }
        }).collect(Collectors.toList());


        return collect;
    }
}
